package com.youseu.taoliving.commodity.service.impl;

import com.youseu.taoliving.commodity.dao.AttrAttrgroupRelationDao;
import com.youseu.taoliving.commodity.dao.AttrgroupDao;
import com.youseu.taoliving.commodity.entity.AttrAttrgroupRelationEntity;
import com.youseu.taoliving.commodity.entity.AttrgroupEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youseu.common.utils.PageUtils;
import com.youseu.common.utils.Query;

import com.youseu.taoliving.commodity.dao.AttrDao;
import com.youseu.taoliving.commodity.entity.AttrEntity;
import com.youseu.taoliving.commodity.service.AttrService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryBasePage(Map<String, Object> params, Long categoryId) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        //只查询基本属性
        wrapper.eq("attr_type",1);
        //前端默认设置categoryId为0
        if(categoryId != 0){
            wrapper.eq("category_id",categoryId);
        }
        String condition = (String) params.get("key");
        if (!StringUtils.isBlank(condition)){
            wrapper.and((obj) -> {
                //条件可能是id name
                wrapper.like("attr_name",condition).or().eq("attr_id",condition);
            });
        }
        IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), wrapper);

        return new PageUtils(page);
    }

    @Override
    public PageUtils querySalesPage(Map<String, Object> params, Long categoryId) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        //只查询销售属性
        wrapper.eq("attr_type",0);
        //前端默认设置categoryId为0
        if(categoryId != 0){
            wrapper.eq("category_id",categoryId);
        }
        String condition = (String) params.get("key");
        if (!StringUtils.isBlank(condition)){
            wrapper.and((obj) -> {
                //条件可能是id name
                wrapper.like("attr_name",condition).or().eq("attr_id",condition);
            });
        }
        IPage<AttrEntity> page =
                this.page(new Query<AttrEntity>().getPage(params), wrapper);

        return new PageUtils(page);
    }

    @Resource
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Transactional
    @Override
    public void saveAttrAndRelation(AttrEntity attrEntity) {
        //保存到属性表
        this.save(attrEntity);
        //如果是基本属性则保存到属性-属性组关联表
        if(attrEntity.getAttrType() == 1 && attrEntity.getAttrGroupId() != null){
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrId(attrEntity.getAttrId());
            attrAttrgroupRelationEntity.setAttrGroupId(attrEntity.getAttrGroupId());
            attrAttrgroupRelationDao.insert(attrAttrgroupRelationEntity);
        }
    }

    @Override
    public List<AttrEntity> getRelationAttr(Long attrgroupId) {
        QueryWrapper<AttrAttrgroupRelationEntity> wrapper =
                new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId);
        //查询commodity_attr_attrgroup_relation表中属性组id关联的信息
        List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationDao.selectList(wrapper);
        //过滤得到属性id
        List<Long> attrIds = relationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        //根据查到的属性id到commodity_attr表查询信息
        if (attrIds.size() == 0){
            return null;
        }
        Collection<AttrEntity> attrEntities = this.listByIds(attrIds);
        return new ArrayList<>(attrEntities);
    }

    @Override
    public void deleteBatchRelation(AttrAttrgroupRelationEntity[] relationEntities) {
        attrAttrgroupRelationDao.deleteBatchRelation(Arrays.asList(relationEntities));
    }


    @Resource
    private AttrgroupDao attrgroupDao;


    //属性组关联基本属性(必须在同一商品分类中)
    @Override
    public PageUtils getAllowRelationAttr(Map<String, Object> params, Long attrGroupId) {
        //根据属性组id得到对应商品分类id
        AttrgroupEntity attrgroupEntity = attrgroupDao.selectById(attrGroupId);
        Long categoryId = attrgroupEntity.getCategoryId();

        //根据商品分类id得到对应所有属性组id
        List<AttrgroupEntity> attrgroupEntities = attrgroupDao.selectList(new QueryWrapper<AttrgroupEntity>().eq("category_id", categoryId));
        List<Long> attrGroupIds = attrgroupEntities.stream().map(AttrgroupEntity::getId).collect(Collectors.toList());

        //根据属性组id得到关联的基本属性id
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities =
                attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", attrGroupIds));
        List<Long> attrIds = attrAttrgroupRelationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());

        //排除已经关联过的属性
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        if (attrIds.size() > 0){
            wrapper.notIn("attr_id",attrIds);
        }
        String condition = (String)params.get("key");
        wrapper.eq("category_id",categoryId).eq("attr_type",1);
        if (StringUtils.isNotBlank(condition)) {
            wrapper.and((obj) -> {
                wrapper.like("attr_name",condition).or().eq("attr_id",condition);
            });
        }
        IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

}